package io

import (
	"gitee.com/injoyai/goutil/oss/io/buf"
	"io"
	"log"
)

// WriteWithDefault 默认发送数据处理函数
func WriteWithDefault(bytes []byte) []byte {
	return WriteWithStartEnd(packageStart, packageEnd)(bytes)
}

// WriteWithStartEnd 根据包头包尾写入
func WriteWithStartEnd(packageStart, packageEnd []byte) func([]byte) []byte {
	return func(bytes []byte) []byte {
		return append(append(packageStart, bytes...), packageEnd...)
	}
}

// ReadWithDefault 默认读取方法,比较全能,不惧粘包
// 读取只有起始帧的数据包时,需要和超时配合使用
func ReadWithDefault(c *Client) (bytes []byte, e error) {
	defer func() {
		if len(bytes) > len(packageStart)+len(packageEnd) &&
			string(bytes[len(packageStart):len(bytes)-len(packageEnd)]) == DefaultPing {
			e = c.write([]byte(DefaultPong))
		}
	}()
	return buf.ReadWithStartEnd(c.Buffer(), packageStart, packageEnd)
}

// ReadWithAll 一次性全部读取,可以代替超时分包
// 适用于读取速度大于写入速度,例如半双工类型
func ReadWithAll(c *Client) (bytes []byte, e error) {
	defer func() {
		if string(bytes) == DefaultPing {
			e = c.write([]byte(DefaultPong))
		}
	}()
	return c.ReadAll()
}

// ReadWithStartEnd 根据包头包尾读取
func ReadWithStartEnd(packageStart, packageEnd []byte) func(c *Client) ([]byte, error) {
	return func(c *Client) ([]byte, error) {
		return buf.ReadWithStartEnd(c.Buffer(), packageStart, packageEnd)
	}
}

// NewReadWithWriter io.Copy 不能设置读取超时
func NewReadWithWriter(writer io.Writer) func(c *Client) ([]byte, error) {
	return func(c *Client) ([]byte, error) {
		_, err := io.Copy(writer, c)
		return nil, err
	}
}

// NewReadWithFrame 新建任意分包
func NewReadWithFrame(f *buf.Frame) func(c *Client) ([]byte, error) {
	return func(c *Client) ([]byte, error) {
		return f.ReadMessage(c.Buffer())
	}
}

// ReadWithLine
// 读取数据,一行一行读取
// 注意非英文容易出现问题,有可能被切割 结尾
func ReadWithLine(r *Client) ([]byte, error) {
	return buf.ReadWithLine(r.Buffer())
}

// PrintWithDefault 默认打印读取数据,打印hex和ascii
func PrintWithDefault(tag string, msg *Message) {
	log.Printf("[%s][%s] 长度:%d HEX:%s 内容:%s\n", tag, msg.Key(), msg.Len(), msg.HEX(), msg.String())
}

// PrintWithHEX 打印读取数据,打印hex
func PrintWithHEX(tag string, msg *Message) {
	log.Printf("[%s][%s] 长度:%d HEX:%s\n", tag, msg.Key(), msg.Len(), msg.HEX())
}

// PrintWithASCII 打印读取数据,打印ascii
func PrintWithASCII(tag string, msg *Message) {
	log.Printf("[%s][%s] %s\n", tag, msg.Key(), msg.String())
}

/*




 */

// readWithStartEnd 默认读取方法
func readWithStartEnd(c *Client, start, end []byte) (bytes []byte, e error) {
	return buf.ReadWithStartEnd(c.Buffer(), start, end)
	readByte := c.ReadByte
	for {
		b, err := readByte()
		if err != nil {
			return bytes, err
		}
		if len(start) > 0 && b == start[0] {
			bs := []byte{b}
			isStart := true
			for _, v := range start[1:] {
				b2, err := readByte()
				if err != nil {
					return bytes, err
				}
				bs = append(bs, b2)
				if b2 != v {
					isStart = false
					break
				}
			}
			if isStart && len(end) > 0 && len(bytes) == 0 {
				bytes = bs
			} else {
				bytes = append(bytes, bs...)
			}
		} else if len(end) > 0 && b == end[0] {
			bs := []byte{b}
			isEnd := true
			for _, v := range end[1:] {
				b2, err := readByte()
				if err != nil {
					return nil, err
				}
				bs = append(bs, b2)
				if b2 != v {
					isEnd = false
					break
				}
			}
			bytes = append(bytes, bs...)
			if isEnd {
				break
			}
		} else {
			bytes = append(bytes, b)
		}
	}
	return bytes, nil
}
